package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.pojo.user.Address;
import com.qingcheng.service.order.CartService;

import com.qingcheng.service.order.OrderService;
import com.qingcheng.service.user.AddressService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 岳剑飞
 * @Company http://www.ithiema.com
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Reference
    private OrderService orderService;

    @Reference
    private CartService cartService;

    /**
     * 从redis中提取购物车列表
     *
     * @return
     */
    @GetMapping("/findCartList")
    public List<Map<String, Object>> findCartList() {
        //1. 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println(username);
        //2. 调用cartService方法,返回购物车列表
        List<Map<String, Object>> cartList = cartService.findCartList(username);
        return cartList;
    }

    /**
     * 添加商品到购物车
     *
     * @param skuId 商品id
     * @param num   数量
     * @return
     */
    @GetMapping("/addItem")
    public Result addItem(String skuId, Integer num) {
        //1. 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println(username);

        cartService.addItem(username, skuId, num);
        return new Result();
    }


    /**
     * 添加商品到购物车 并重定向购物车页面
     *
     * @param response
     * @param skuId
     * @param num
     * @throws Exception
     */
    @RequestMapping("/buy")
    public void buy(HttpServletResponse response, String skuId, Integer num) throws Exception {

        // 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        // 调用方法  存储到购物车
        cartService.addItem(username, skuId, num);
        response.sendRedirect("/cart.html");
    }

    /**
     * 更改购物车选中状态
     *
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping("/updateChecked")
    public Result updateChecked(String skuId, boolean checked) {
        // 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //调用方法修改购物车选中状态
        cartService.updateChecked(username, skuId, checked);
        return new Result();
    }

    /**
     * 删除选中的购物车
     *
     * @return
     */
    @GetMapping("/deleteCheckedCart")
    public Result deleteCheckedCart() {
        // 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.deleteCheckedCart(username);
        return new Result();
    }

    /**
     * 计算当前选中的购物车的优惠金额
     *
     * @return
     */
    @GetMapping("/preferential")
    public Map preferential() {
        // 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //获取当前购物车商品的优惠金额
        int preferential = cartService.preferential(username);
        Map map = new HashMap();
        map.put("preferential", preferential);
        return map;
    }

    /**
     * 获取刷新单价后的购物车列表
     *
     * @return
     */
    @GetMapping("/findNewOrderItemList")
    public List<Map<String, Object>> findNewOrderItemList() {
        // 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return cartService.findNewOrderItemList(username);
    }

    @Reference
    private AddressService addressService;

    /**
     * 根据用户查询地址列表
     *
     * @return
     */
    @GetMapping("/findAddressList")
    public List<Address> findAddressList() {
        // 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<Address> addresses = addressService.findByUsername(username);
        System.out.println("走这里,哈哈哈");
        return addresses;
    }

    @PostMapping("/saveOrder")
    public Map<String, Object> saveOrder(@RequestBody Order order) {
        // 获取当前登录用户
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        order.setUsername(username);
        //新增订单主表和明细表
        return orderService.add(order);
    }
}
